home *** CD-ROM | disk | FTP | other *** search
/ Winzipper / Winzipper_ISO.iso / programming / oracle7 7.2 / DB / UTIL72 / PRVTUTIL.SQL < prev    next >
Encoding:
Text File  |  1995-05-09  |  31.2 KB  |  851 lines

  1. rem 
  2. rem $Header: prvtutil.sql 7020200.5 95/03/29 18:10:03 cli Generic<base> $ 
  3. rem 
  4. Rem
  5. Rem    NAME
  6. Rem      prvtutil.sql - packages of various utility procedures
  7. Rem    DESCRIPTION
  8. Rem      These are private functions to be released in PL/SQL binary form.
  9. Rem      This file contains various packages:
  10. Rem         dbms_transaction    - transaction commands
  11. Rem         dbms_session    - alter session commands
  12. Rem         dbms_ddl        - ddl commands
  13. Rem         dbms_utility    - helpful utilities
  14. Rem         dbms_application_info - application information registration
  15. Rem         dbms_space            - space analysis utilities
  16. Rem    RETURNS
  17. Rem 
  18. Rem    NOTES
  19. Rem      The procedural option is needed to use these facilities.
  20. Rem
  21. Rem      All of the packages below run with the privileges of calling user,
  22. Rem      rather than the package owner ('sys').
  23. Rem
  24. Rem      Procedure 'dbms_ddl.alter_compile' and 'dbms_ddl.analyze_object
  25. Rem      commit the current transaction, perform the compilation, and 
  26. Rem      then commit again.
  27. Rem 
  28. Rem      The dbms_utility package is run-as-caller (psdicd.c) only for
  29. Rem      its name_resolve, compile_schema and analyze_schema
  30. Rem      procedures.  This package is not run-as-caller
  31. Rem      w.r.t. SQL (psdpgi.c) so that the SQL works correctly (runs as
  32. Rem      SYS).  The privileges are checked via dbms_ddl.
  33. Rem
  34. Rem    MODIFIED   (MM/DD/YY)
  35. Rem     bhirano    12/23/94 -  merge changes from branch 1.1.710.7
  36. Rem     jstamos    11/11/94 -  merge changes from branch 1.1.710.6 (#239271)
  37. Rem     rtaranto   10/28/94 -  merge changes from branch 1.1.710.5
  38. Rem     rtaranto   10/28/94 -  Change context to be binary_integer
  39. Rem     jloaiza    09/07/94 -  dbms_registration -> dbms_application_info
  40. Rem     atsukerm   06/22/94 -  DBMS_SPACE implementation
  41. Rem     wmaimone   05/26/94 -  #186155 add public synoyms for dba_
  42. Rem     jloaiza    06/08/94 -  add dbms_registration
  43. Rem     jloaiza    04/08/94 -  add dbms_system
  44. Rem     dsdaniel   04/07/94 -  merge changes from branch 1.1.710.2
  45. Rem     wmaimone   04/07/94 -  merge changes from branch 1.1.710.3
  46. Rem     adowning   03/29/94 -  merge changes from branch 1.1.710.1
  47. Rem     wmaimone   02/07/94 -  add set close_cached_open_cursors to dbms_sessio
  48. Rem     dsdaniel   02/04/94 -  dbms_util.port_string icd
  49. Rem     adowning   02/04/94 -  Branch_for_patch
  50. Rem     adowning   02/04/94 -  Creation
  51. Rem     adowning   02/02/94 -  split file into public / private binary files
  52. Rem     rjenkins   10/28/93 -  make comma_to_table more consistent
  53. Rem     rjenkins   10/12/93 -  adding comma_to_table
  54. Rem     rjenkins   09/03/93 -  adding name_parse
  55. Rem     hjakobss   07/15/93 -  bug 170473
  56. Rem     hjakobss   07/13/93 -  bug 169577
  57. Rem     dsdaniel   03/12/93 -  local_tid, step_id functions for replication  
  58. Rem     mmoore     01/11/93 -  merge changes from branch 1.37.312.1 
  59. Rem     mmoore     01/05/93 - #(145287) add another exception for discrete mode
  60. Rem     mmoore     12/11/92 -  disable set_role in stored procs 
  61. Rem     rkooi      11/24/92 -  fixes per Peter 
  62. Rem     rkooi      11/21/92 -  get rid of error argument to name_resolve 
  63. Rem     tpystyne   11/20/92 -  fix compile_all and analyze_schema 
  64. Rem     rkooi      11/16/92 -  fix set_label 
  65. Rem     rkooi      11/16/92 -  fix comments 
  66. Rem     rkooi      11/13/92 -  add name_res procedure 
  67. Rem     tpystyne   11/07/92 -  make analyze parameters optional 
  68. Rem     mmoore     11/04/92 -  add new analyze options 
  69. Rem     ghallmar   11/03/92 -  add dbms_transaction.purge_mixed 
  70. Rem     rkooi      10/30/92 -  get rid of caller_id and unique_stmt_id 
  71. Rem     rkooi      10/26/92 -  owner -> schema for SQL2 
  72. Rem     rkooi      10/25/92 -  bug 135880 
  73. Rem     mmoore     10/13/92 - #(131686) change messages 2074,4092,0034 
  74. Rem     rkooi      10/02/92 -  compile_all fix 
  75. Rem     mmoore     10/02/92 -  change pls_integer to binary_integer 
  76. Rem     tpystyne   10/01/92 -  fix Bob's mistakes 
  77. Rem     tpystyne   09/28/92 -  disallow commit/rollback force in rpc and trigge
  78. Rem     mmoore     09/25/92 - #(130566) don't allow set_nls or set_role in trig
  79. Rem     tpystyne   09/23/92 -  rename analyze to analyze_object 
  80. Rem     rkooi      08/24/92 -  handle delimited id's in alter_compile 
  81. Rem     tpystyne   08/06/92 -  add analyze_schema 
  82. Rem     epeeler    07/29/92 -  add function to get time 
  83. Rem     rkooi      06/25/92 -  workaround pl/sql bug with 'in' in SQL
  84. Rem     rkooi      06/03/92 -  add 'get unique session id' 
  85. Rem     jcohen     05/28/92 -  add = to alter session set label 
  86. Rem     jloaiza    05/12/92 -  add discrete 
  87. Rem     rkooi      04/22/92 -  put in checks for execute_sql for triggs, stored
  88. Rem     mmoore     04/14/92 -  move begin_oltp to package transaction 
  89. Rem     rkooi      04/06/92 -  merge changes from branch 1.4.300.1 
  90. Rem     rkooi      04/01/92 -  Creation - split/recombined from other files
  91. Rem     mroberts   02/21/92 -  call alter_compile, not sql_ddl 
  92. Rem     rkooi      02/06/92 -  testing 
  93. Rem     rkooi      02/03/92 -  compilation errors 
  94. Rem     rkooi      01/16/92 -  Creation 
  95.  
  96. REM ********************************************************************
  97. REM THESE PACKAGES MUST NOT BE MODIFIED BY THE CUSTOMER.  DOING SO
  98. REM COULD CAUSE INTERNAL ERRORS AND SECURITY VIOLATIONS IN THE
  99. REM RDBMS.  SPECIFICALLY, THE PSD* AND EXECUTE_SQL ROUTINES MUST NOT BE
  100. REM CALLED DIRECTLY BY ANY CLIENT AND MUST REMAIN PRIVATE TO THE PACKAGE BODY.
  101. REM ********************************************************************
  102.  
  103. create or replace package body dbms_transaction is
  104.   -- internal icd:  perform DDL statement
  105.   procedure execute_sql(coord_sess_ok binary_integer, forms_ok binary_integer,
  106.       trigger_ok binary_integer, procedure_ok binary_integer, stmt varchar2, 
  107.       error_hint varchar2);
  108.     pragma interface (C, execute_sql);                     -- 6 (see psdicd.c)
  109.  
  110.   -- internal icd: get transaction id
  111.   function ltid_icd(create_txn binary_integer) return varchar2;
  112.     pragma interface (c, ltid_icd);                        -- 7 (see psdicd.c)
  113.  
  114.   -- internal icd: get step id
  115.   function step_icd return number;
  116.     pragma interface (c, step_icd);                        -- 8 (see psdicd.c)
  117.  
  118.   procedure commit_force(xid varchar2, scn varchar2 default null) is
  119.   begin
  120.     if scn is NULL then
  121.       execute_sql(0, 0, 0, 1, 'commit force ''' || xid || '''', 'COMMIT');
  122.     else 
  123.       execute_sql(0, 0, 0, 1, 'commit force ''' || xid || ''' ''' ||
  124.         scn || '''', 'COMMIT');
  125.     end if;
  126.   end;
  127.  
  128.   procedure rollback_force(xid varchar2) is
  129.   begin
  130.     execute_sql(0, 0, 0, 1, 'rollback force ''' || xid || '''', 'ROLLBACK');
  131.   end;
  132.  
  133.   procedure advise_commit is
  134.   begin
  135.     execute_sql(1, 1, 1, 1, 'alter session advise commit', 'ADVISE COMMIT');
  136.   end;
  137.  
  138.   procedure advise_rollback is
  139.   begin
  140.     execute_sql(1, 1, 1, 1, 'alter session advise rollback', 
  141.                  'ADVISE ROLLBACK');
  142.   end;
  143.  
  144.   procedure advise_nothing is
  145.   begin
  146.     execute_sql(1, 1, 1, 1, 'alter session advise nothing','ADVISE NOTHING');
  147.   end;
  148.  
  149.   procedure commit_comment(cmnt varchar2) is
  150.   begin
  151.     execute_sql(0, 0, 0, 1, 'commit comment ' || '''' || cmnt || '''', 
  152.                 'COMMIT');
  153.   end;
  154.  
  155.   procedure read_only is
  156.   begin
  157.     execute_sql(0, 1, 0, 1, 'set transaction read only', 'SET TRANSACTION');
  158.   end;
  159.  
  160.   procedure read_write is
  161.   begin
  162.     execute_sql(0, 1, 0, 1, 'set transaction read write', 'SET TRANSACTION');
  163.   end;
  164.  
  165.   procedure use_rollback_segment(rb_name varchar2) is
  166.   begin
  167.     execute_sql(0, 1, 0, 1, 'set transaction use rollback segment ' || rb_name,
  168.        'SET TRANSACTION');
  169.   end;
  170.  
  171.   procedure purge_mixed(xid varchar2) is
  172.     transaction_not_found exception;
  173.   begin
  174.     use_rollback_segment('SYSTEM');
  175.     delete from sys.pending_trans$ where status = 'D' and local_tran_id = xid;
  176.     if sql%rowcount = 1 then
  177.       delete from sys.pending_sessions$ where local_tran_id = xid;
  178.       delete from sys.pending_sub_sessions$ where local_tran_id = xid;
  179.     else
  180.       raise transaction_not_found;
  181.     end if;
  182.   end;
  183.  
  184.   FUNCTION local_transaction_id(create_transaction BOOLEAN := FALSE)
  185.     RETURN VARCHAR2 is
  186.   begin
  187.     if create_transaction then
  188.       return(ltid_icd(1));
  189.     else
  190.       return(ltid_icd(0));
  191.     end if;
  192.   end;
  193.  
  194.   FUNCTION step_id RETURN NUMBER is
  195.   begin
  196.    return(step_icd);
  197.   end;
  198.  
  199. end;
  200. /
  201.  
  202. create or replace package body dbms_session is
  203.   -- internal icd:  perform DDL statement
  204.   procedure execute_sql(coord_sess_ok binary_integer, forms_ok binary_integer,
  205.       trigger_ok binary_integer, procedure_ok binary_integer, stmt varchar2, 
  206.       error_hint varchar2);
  207.     pragma interface (C, execute_sql);                      -- 1 (see psdicd.c)
  208.  
  209.   -- deinstantiate all pkgs in this session
  210.   procedure psddin;                        -- 2 (see psdicd.c)
  211.     pragma interface (C, psddin);
  212.  
  213.   -- get an id that is unique for all sessions in this database
  214.   function psduis return varchar2;                -- 3 (see psdicd.c)
  215.     pragma interface (C, psduis);
  216.  
  217.   -- is given role enabled?
  218.   function psdire(rolename varchar2) return binary_integer; -- 4 (see psdicd.c)
  219.     pragma interface (C, psdire);
  220.  
  221.   -- free unused memory from user heap
  222.   procedure psdfmr(heapno binary_integer, recurse binary_integer);
  223.     pragma interface (C, psdfmr);                           -- 5 (see psdicd.c)
  224.  
  225.   procedure set_role(role_cmd varchar2) is
  226.   begin
  227.     execute_sql(1, 1, 0, 0, 'set role ' || role_cmd, 'SET ROLE');
  228.   end;
  229.  
  230.   procedure set_sql_trace(sql_trace boolean) is
  231.   begin
  232.     if sql_trace then
  233.       execute_sql(1, 1, 1, 1, 'alter session set sql_trace true', 
  234.         'SET SQL_TRACE');
  235.     else
  236.       execute_sql(1, 1, 1, 1, 'alter session set sql_trace false',
  237.         'SET SQL_TRACE');
  238.     end if;
  239.   end;
  240.  
  241.   procedure set_nls(param varchar2, value varchar2) is
  242.     ddl_error exception;
  243.   begin
  244.     /* prevent sneaking in other 'alter session set' commands */
  245.     if substr(upper(param),1,4) <> 'NLS_' 
  246.         or length(value) > 20 then
  247.       raise ddl_error;
  248.     end if;
  249.     execute_sql(0, 1, 0, 1, 'alter session set ' || param || ' = ' || value,
  250.        'SET NLS');
  251.   end;
  252.  
  253.   procedure close_database_link(dblink varchar2) is
  254.   begin
  255.     execute_sql(1, 1, 1, 1, 'alter session close database link ' || dblink,
  256.       'CLOSE DATABASE LINK');
  257.   end;
  258.  
  259.   procedure set_label(lbl varchar2) is
  260.   begin
  261.     if upper(lbl) = 'DBHIGH' or upper(lbl) = 'DBLOW' then
  262.       execute_sql(0, 1, 1, 1, 'alter session set label = ' || lbl, 'SET LABEL');
  263.     else
  264.       execute_sql(0, 1, 1, 1, 'alter session set label = ''' || lbl || '''',
  265.         'SET LABEL');
  266.     end if;
  267.   end;
  268.  
  269.   procedure set_mls_label_format(fmt varchar2) is
  270.   begin
  271.     execute_sql(0, 1, 1, 1,
  272.       'alter session set mls_label_format = ''' || fmt || '''', 
  273.       'SET MLS LABEL FORMAT');
  274.   end;
  275.  
  276.   procedure reset_package is
  277.   begin
  278.     psddin;
  279.   end;
  280.  
  281.   function unique_session_id return varchar2 is
  282.   begin
  283.     return psduis;
  284.   end;
  285.  
  286.   function is_role_enabled(rolename varchar2) return boolean is
  287.   begin
  288.     if psdire(rolename) = 1 then
  289.       return TRUE;
  290.     else
  291.       return FALSE;
  292.     end if;
  293.   end;
  294.  
  295.   procedure set_close_cached_open_cursors(close_cursors boolean) is
  296.   begin
  297.     if close_cursors then
  298.       execute_sql(1, 1, 1, 1, 
  299.     'alter session set close_cached_open_cursors = true',
  300.         'SET CLOSE_CACHED_OPEN_CURSORS'); 
  301.    else
  302.       execute_sql(1, 1, 1, 1, 
  303.     'alter session set close_cached_open_cursors = false',
  304.         'SET CLOSE_CACHED_OPEN_CURSORS');
  305.     end if;
  306.   end;
  307.  
  308.   procedure free_unused_user_memory(heapno binary_integer, recurse boolean) is
  309.     recval binary_integer;
  310.   begin
  311.     if recurse then                      -- set recurse to a binary_integer
  312.       recval := 1;
  313.     else 
  314.       recval := 0;
  315.     end if;
  316.       
  317.     psdfmr(heapno,recval);               -- invoke the icd
  318.   end;
  319.  
  320.   procedure free_unused_user_memory is
  321.   begin 
  322.     free_unused_user_memory(0, TRUE);       -- call 'internal' function
  323.   end;
  324.  
  325. end;
  326. /
  327.  
  328. create or replace package body dbms_ddl is
  329.   NOT_EXIST0 exception;
  330.   pragma EXCEPTION_INIT(NOT_EXIST0, -942);
  331.   NOT_EXIST1 exception;
  332.   pragma EXCEPTION_INIT(NOT_EXIST1, -4042);
  333.   NOT_EXIST2 exception;
  334.   pragma EXCEPTION_INIT(NOT_EXIST2, -4043);
  335.   NOT_EXIST3 exception;
  336.   pragma EXCEPTION_INIT(NOT_EXIST3, -6564);
  337.   NOT_EXIST4 exception;
  338.   pragma EXCEPTION_INIT(NOT_EXIST4, -943);
  339.   NOT_EXIST5 exception;
  340.   pragma EXCEPTION_INIT(NOT_EXIST5, -1418);
  341.   NO_PRIV    exception;
  342.   pragma EXCEPTION_INIT(NO_PRIV, -1031);
  343.  
  344.   -- internal icd:  perform DDL statement
  345.   procedure execute_sql(coord_sess_ok binary_integer, forms_ok binary_integer,
  346.       trigger_ok binary_integer, procedure_ok binary_integer, stmt varchar2, 
  347.       error_hint varchar2);
  348.     pragma interface (C, execute_sql);                      -- 1 (see psdicd.c)
  349.  
  350.   procedure alter_compile(type varchar2, schema varchar2, name varchar2) is
  351.     ptype      varchar2(20);
  352.     pschema    varchar2(30);
  353.     pname      varchar2(65);
  354.     owner      varchar2(30);
  355.     part1      varchar2(30);
  356.     part2      varchar2(30);
  357.     dblink     varchar2(30);
  358.     part1_type number;
  359.     objno      number;
  360.   begin
  361.     pschema := schema;
  362.     pname := name;
  363.     if pschema IS NOT NULL then
  364.       pname := pschema || '"."' || pname;
  365.     end if;
  366.     pname := '"' || pname || '"';
  367.  
  368.     begin
  369.       /* name resolve to make sure the object is not a synonym for something
  370.          that we depend on, an hence would cause a deadlock */
  371.       dbms_utility.name_resolve(pname, 1, owner, part1, part2, dblink,
  372.                                 part1_type, objno);
  373.     exception when not_exist3 or no_priv then
  374.       raise_application_error(-20000, 'Unable to compile ' || type || ' ' 
  375.         || pname || ', insufficient privileges or does not exist');
  376.     end;
  377.  
  378.     if (objno is null or dblink is not null) then
  379.       raise_application_error(-20001, 'cannot compile remote ' || type ||
  380.         ' ' || pname);
  381.     end if;
  382.     if owner = 'SYS' 
  383.          and part1 in ('DBMS_STANDARD', 'STANDARD', 'DBMS_DDL') then 
  384.       return;
  385.     end if;
  386.  
  387.     ptype := upper(type);
  388.     commit; -- this commit will fail if in coordinated sesson or
  389.             -- if forms has done 'alter session disable commits ...'
  390.         -- so the 1st two args to execute_sql below are irrelevant
  391.     begin
  392.       if ptype = 'PACKAGE BODY' then
  393.         execute_sql(0, 0, 0, 1, 'alter package ' || pname || ' compile body',
  394.           'ALTER PACKAGE COMPILE');
  395.       elsif ptype = 'PACKAGE' then
  396.         execute_sql(0, 0, 0, 1, 'alter package ' || pname || ' compile',
  397.           'ALTER PACKAGE COMPILE');
  398.       elsif ptype = 'PROCEDURE' or ptype = 'FUNCTION' then
  399.         execute_sql(0, 0, 0, 1, 'alter ' || ptype || ' ' || pname || ' compile',
  400.           'ALTER PROCEDURE COMPILE');
  401.       else
  402.         raise_application_error(-20001, 'bad value for object type: '||ptype);
  403.       end if;
  404.     exception when not_exist1 or not_exist2 or no_priv then
  405.       raise_application_error(-20000, 'Unable to compile ' || type || ' ' 
  406.         || pname || ', insufficient privileges or does not exist');
  407.     end;
  408.     commit;
  409.   end;
  410.  
  411.   procedure analyze_object
  412.     (type varchar2, schema varchar2, name varchar2, method varchar2,
  413.      estimate_rows number default null, 
  414.      estimate_percent number default null) is
  415.     oname  varchar2(65);
  416.     sample varchar2(30) := '';
  417.   begin
  418.     oname := name;
  419.     if schema IS NOT NULL then
  420.       oname := schema || '"."' || name;
  421.     end if;
  422.     oname := '"' || oname || '"';
  423.  
  424.     commit;
  425.    
  426.     -- don't analyze fet$ and uet$, could possibly cause deadlocks
  427.     if schema = 'SYS' and name in ('UET$', 'FET$') then return; end if;
  428.  
  429.     if upper(method) = 'ESTIMATE' then
  430.       if estimate_rows != 0 then
  431.         sample := 'sample '||estimate_rows||' rows';
  432.       elsif estimate_percent != 0 then 
  433.         sample := 'sample '||estimate_percent||' percent';
  434.       end if;
  435.     end if;
  436.  
  437.     begin
  438.       if upper(type) = 'CLUSTER' then
  439.         execute_sql(0, 0, 0, 1,
  440.           'analyze cluster '||oname||' '||method||' statistics '||sample, 
  441.           'ANALYZE CLUSTER');
  442.       elsif upper(type) = 'TABLE' then
  443.         execute_sql(0, 0, 0, 1,
  444.           'analyze table '||oname||' '||method||' statistics '||sample,
  445.           'ANALYZE TABLE');
  446.       elsif upper(type) = 'INDEX' then
  447.         execute_sql(0, 0, 0, 1,
  448.           'analyze index '||oname||' '||method||' statistics '||sample,
  449.           'ANALYZE INDEX');
  450.       else
  451.         raise_application_error(-20001, 'bad value for object type: ' || type);
  452.       end if;
  453.     exception when not_exist0 or not_exist1 or not_exist2 or not_exist4 or
  454.          not_exist5 or no_priv then
  455.       raise_application_error(-20000, 'Unable to analyze ' || type || ' ' 
  456.         || oname || ', insufficient privileges or does not exist');
  457.     end;
  458.     commit;
  459.   end;
  460.  
  461. end;
  462. /
  463.  
  464. create or replace view order_object_by_dependency (dlevel, object_id) as
  465.        select max(level), object_id from public_dependency
  466.        connect by object_id = prior referenced_object_id
  467.        group by object_id
  468. /
  469.  
  470. create or replace view dba_analyze_objects (owner, object_name, object_type) as
  471.        select u.name, o.name, decode(o.type, 2, 'TABLE', 3, 'CLUSTER')
  472.        from sys.user$ u, sys.obj$ o, sys.tab$ t
  473.        where o.owner# = u.user#
  474.        and   o.obj# = t.obj# (+)
  475.        and   t.clu# is null
  476.        and   o.type in (2,3)
  477. /
  478.  
  479. create or replace package body dbms_utility is
  480.   function is_parallel return binary_integer;
  481.     pragma interface (C, is_parallel);                  -- 3 (see psdicd.c)
  482.   function icd_get_time return binary_integer;
  483.     pragma interface (C, icd_get_time);                -- 4 (see psdicd.c)
  484.   procedure icd_name_res(name in varchar2, context in binary_integer, 
  485.       schema out varchar2, part1 out varchar2, part2 out varchar2,
  486.       dblink out varchar2, part1_type out binary_integer,
  487.       object_number out binary_integer);
  488.     pragma interface (C, icd_name_res);                -- 5 (see psdicd.c)
  489.   procedure icd_name_tokenize( name    in  varchar2,
  490.                        a       out varchar2,
  491.                        b       out varchar2,
  492.                            c       out varchar2,
  493.                                dblink  out varchar2, 
  494.                                nextpos out binary_integer);
  495.     pragma interface (C, icd_name_tokenize);                -- 6 (see psdicd.c)
  496.   FUNCTION psdpor RETURN VARCHAR2;
  497.     pragma interface (C, psdpor);                           -- 7 (see psdicd.c)
  498.  
  499.   function icd_dba(file binary_integer, block binary_integer) 
  500.        return binary_integer;
  501.     pragma interface (C, icd_dba);                        -- 8 (see psdicd.c)
  502.  
  503.   function icd_dba_file(dba binary_integer) return binary_integer;
  504.     pragma interface (C, icd_dba_file);                        -- 9 (see psdicd.c)
  505.  
  506.   function icd_dba_block(dba binary_integer) return binary_integer;
  507.     pragma interface (C, icd_dba_block);                    -- 10(see psdicd.c)
  508.  
  509.   procedure name_resolve(name in varchar2, context in number,
  510.     schema out varchar2, part1 out varchar2, part2 out varchar2,
  511.     dblink out varchar2, part1_type out number, object_number out number) is
  512.   begin
  513.     if context != 1 and context != 3 then
  514.   raise_application_error(-20005, 'ORU-10034: context argument must be 1 or 3');
  515.     end if;
  516.     icd_name_res(name, context, schema, part1, part2, dblink, part1_type,
  517.       object_number);
  518.   end;
  519.  
  520.   procedure name_tokenize( name    in  varchar2, 
  521.                    a       out varchar2,
  522.                           b       out varchar2,
  523.                        c       out varchar2,
  524.                    dblink  out varchar2, 
  525.                    nextpos out binary_integer) is
  526.   begin
  527.     icd_name_tokenize( name, a, b, c, dblink, nextpos );
  528.   end;
  529.  
  530.   -- Make a PL/SQL table out of a comma-separated list of names
  531.   --   names :== a [. b [. c ]][ @ d ]
  532.   --   list :== name [ , list ]
  533.   --   Comma_to_table takes a non-empty comma-separated list.  
  534.   --   Anything other than a comma-separated list is rejected.
  535.   --   Commas inside doublequotes do not count.
  536.   --   A PL/SQL table is returned, with values 1..n, and n+1 is null.
  537.   --   The values in tab are cut from the original list; no transformations.
  538.   PROCEDURE comma_to_table( list   IN  VARCHAR2, 
  539.                             tablen OUT BINARY_INTEGER,
  540.                             tab    OUT uncl_array ) IS
  541.     nextpos    BINARY_INTEGER;
  542.     oldpos     BINARY_INTEGER;
  543.     done       BOOLEAN;
  544.     i          BINARY_INTEGER;
  545.     len        BINARY_INTEGER;
  546.     dummy      VARCHAR2(128);
  547.   BEGIN
  548.     -- get ready
  549.     nextpos  := 1;
  550.     done     := FALSE;
  551.     i        := 1;
  552.     len      := NVL(LENGTHB(list),0);
  553.  
  554.     WHILE NOT done LOOP
  555.       oldpos := nextpos;
  556.       dbms_utility.name_tokenize( SUBSTRB(list,oldpos),
  557.                            dummy, dummy, dummy, dummy, nextpos );
  558.       tab(i) := SUBSTRB( list, oldpos, nextpos );
  559.       nextpos := oldpos + nextpos;
  560.       IF nextpos > len THEN
  561.         done := TRUE;
  562.       ELSIF SUBSTRB(list,nextpos,1) = ',' then
  563.         nextpos := nextpos + 1;
  564.       ELSE 
  565.         raise_application_error( -20001, 
  566.           'comma-separated list invalid near ' || SUBSTRB(list,nextpos-2,5));
  567.       END IF;
  568.       i := i + 1;
  569.     END LOOP;
  570.  
  571.     -- handle the end of the list
  572.     tab(i) := NULL;
  573.     tablen := i-1;
  574.   END;
  575.  
  576.  
  577.   -- Make a comma-separated list out of a PL/SQL table
  578.   --   table_to_comma takes a PL/SQL table, 1..n, terminated with n+1 null.
  579.   --   table_to_comma returns a comma-separated list and 
  580.   --     the number of elements found in the table (n).
  581.   --   Note that ',,,' || ',' || ',,,' = ',,,,,,,'.
  582.   PROCEDURE table_to_comma( tab    IN  uncl_array, 
  583.                             tablen OUT BINARY_INTEGER,
  584.                             list   OUT VARCHAR2) IS
  585.     temp  VARCHAR2(6500) := '';
  586.     i     BINARY_INTEGER :=  1;
  587.   BEGIN
  588.     IF tab(i) IS NOT NULL THEN
  589.       temp := tab(i);
  590.       i    := i + 1;
  591.       WHILE tab(i) IS NOT NULL LOOP
  592.         temp := temp || ',' || tab(i);
  593.         i := i + 1;
  594.       END LOOP;
  595.     END IF;
  596.     tablen := i-1;
  597.     list   := temp;
  598.   EXCEPTION
  599.     WHEN NO_DATA_FOUND THEN
  600.       tablen := i-1;
  601.       list   := temp; 
  602.   END;
  603.  
  604.   function get_time return number is
  605.   begin
  606.     return icd_get_time;
  607.   end;
  608.  
  609.   function is_parallel_server return boolean is
  610.   begin
  611.     if is_parallel = 1 then
  612.       return TRUE;
  613.     else
  614.       return FALSE;
  615.     end if;
  616.   end;
  617.  
  618.   procedure compile_schema (schema varchar2) is
  619.     NOT_EXIST_OR_NO_PRIV exception;
  620.     pragma EXCEPTION_INIT(NOT_EXIST_OR_NO_PRIV, -20000);
  621.  
  622.     cursor c1(schema varchar2) is 
  623.       select a.object_type, a.object_name, a.status
  624.       from sys.order_object_by_dependency p, sys.dba_objects a
  625.       where p.object_id = a.object_id
  626.         and a.owner = c1.schema
  627.                /* need PACKAGE BODY in clause below so that dependency ordering
  628.          is done correctly.  But since compiling a package spec also
  629.          compiles the body (we don't have an 'alter package foo compile
  630.      spec only' command), skip over package bodies in the loop below.
  631.          Then if there are any invalid bodies take care of them in a 
  632.          final pass */
  633.       and (a.object_type = 'FUNCTION' or a.object_type = 'PROCEDURE' or
  634.            a.object_type = 'PACKAGE' or a.object_type = 'PACKAGE BODY')
  635.       order by dlevel desc;
  636.   begin
  637.     for rec in c1(schema) loop
  638.       if rec.object_type <> 'PACKAGE BODY' and (schema <> 'SYS' or
  639.           rec.object_name not in ('DBMS_UTILITY', 'DBMS_SESSION',
  640.           'DBMS_TRANSACTION')) then
  641.         begin
  642.           dbms_ddl.alter_compile(rec.object_type, schema, rec.object_name);
  643.         exception when NOT_EXIST_OR_NO_PRIV then
  644.           raise_application_error(-20000,
  645.             'You have insufficient privileges for an object in this schema.');
  646.         end;
  647.       end if;
  648.     end loop;
  649.  
  650.     -- now look for any bodies which were invalidated after their
  651.     -- compilation due to compilation of other specs.  If we had an
  652.     -- 'alter package foo compile spec only' command then we wouldn't need 
  653.     -- this loop as we could take care of bodies in the loop above without
  654.     -- causing duplicate compiles for all bodies.
  655.     for rec in c1(schema) loop
  656.       if rec.object_type = 'PACKAGE BODY' and rec.status = 'INVALID' and 
  657.           (schema <> 'SYS' or rec.object_name not in ('DBMS_UTILITY',
  658.            'DBMS_SESSION', 'DBMS_TRANSACTION')) then
  659.         begin
  660.           dbms_ddl.alter_compile(rec.object_type, schema, rec.object_name);
  661.         exception when NOT_EXIST_OR_NO_PRIV then
  662.           raise_application_error(-20000,
  663.             'You have insufficient privileges for an object in this schema.');
  664.         end;
  665.       end if;
  666.     end loop;
  667.  
  668.     dbms_session.reset_package;
  669.   end;
  670.  
  671.   procedure analyze_schema(schema varchar2, method varchar2, 
  672.                            estimate_rows number default null, 
  673.                            estimate_percent number default null) is
  674.     NOT_EXIST_OR_NO_PRIV exception;
  675.     pragma EXCEPTION_INIT(NOT_EXIST_OR_NO_PRIV, -20000);
  676.  
  677.     cursor c1(schema varchar2) is 
  678.       select object_name, object_type
  679.       from sys.dba_analyze_objects
  680.       where owner = c1.schema
  681.       order by object_type, object_name;
  682.   begin
  683.     -- analyze all clusters and non-clustered tables in the schema       
  684.     for rec in c1(schema) loop
  685.         begin
  686.           dbms_ddl.analyze_object(rec.object_type, schema, rec.object_name,
  687.                                   method, estimate_rows, estimate_percent);
  688.         exception when NOT_EXIST_OR_NO_PRIV then
  689.           raise_application_error(-20000,
  690.             'You have insufficient privileges for an object in this schema.');
  691.         end;
  692.     end loop;
  693.   end;
  694.  
  695.   FUNCTION port_string RETURN VARCHAR2 IS
  696.   BEGIN
  697.     RETURN(psdpor);
  698.   END port_string;
  699.  
  700.   function make_data_block_address(file number, block number) return number is
  701.   begin 
  702.     return (icd_dba(file,block));
  703.   end;
  704.  
  705.   function data_block_address_file(dba number) return number is
  706.   begin
  707.     return (icd_dba_file(dba));
  708.   end;
  709.  
  710.   function data_block_address_block(dba number) return number is
  711.   begin
  712.     return (icd_dba_block(dba));
  713.   end;
  714.  
  715. END dbms_utility;
  716. /
  717.  
  718.  
  719. create or replace package body dbms_system is
  720.  
  721.   procedure set_ev_icd(sid binary_integer, ser binary_integer, 
  722.                ev binary_integer, lev binary_integer, name varchar2);
  723.     pragma interface (C, set_ev_icd);                      -- 1 (see psdicd.c)
  724.   --  This is an internally used routine that should never be called by users.
  725.  
  726.   procedure read_ev_icd(iev binary_integer, oev out binary_integer);
  727.     pragma interface (C, read_ev_icd);                     -- 2 (see psdicd.c)
  728.   --  This is an internally used routine that should never be called by users.
  729.  
  730.   procedure set_sql_trace_in_session(sid number, serial# number, 
  731.                                  sql_trace boolean) is
  732.   begin
  733.     if sql_trace 
  734.     then set_ev(sid, serial#, 10046, 1, '');
  735.     else set_ev(sid, serial#, 10046, 0, '');
  736.     end if;
  737.   end;
  738.  
  739.   -- set event in sesssion
  740.   procedure set_ev(si binary_integer, se binary_integer, 
  741.                ev binary_integer, le binary_integer, nm varchar2) is
  742.     begin set_ev_icd(si,se,ev,le,nm); end;
  743.  
  744.   -- read value of event
  745.   procedure read_ev(iev binary_integer, oev out binary_integer) is
  746.     begin read_ev_icd(iev, oev); end;
  747.  
  748. end dbms_system;
  749. /
  750.  
  751.  
  752.  
  753. create or replace package body dbms_application_info is
  754.   procedure icd_set_module(module_name varchar2, action_name varchar2);
  755.     pragma interface (C, icd_set_module);               -- 1 (see psdicd.c)
  756.  
  757.   procedure icd_set_action(action_name varchar2);
  758.     pragma interface (C, icd_set_action);               -- 2 (see psdicd.c)
  759.  
  760.   procedure icd_read_module(module_name out varchar2);
  761.     pragma interface (C, icd_read_module);              -- 3 (see psdicd.c)
  762.  
  763.   procedure icd_read_action(action_name out varchar2);
  764.     pragma interface (C, icd_read_action);              -- 4 (see psdicd.c)
  765.  
  766.   procedure icd_set_client_info(client_info varchar2);
  767.     pragma interface (C, icd_set_client_info);             -- 5 (see psdicd.c)
  768.  
  769.   procedure icd_read_info(client_info out varchar2);
  770.     pragma interface (C, icd_read_info);                -- 6 (see psdicd.c)
  771.  
  772.  
  773.   procedure set_module(module_name varchar2, action_name varchar2) is
  774.     begin icd_set_module(module_name, action_name); end;
  775.   
  776.   procedure set_action(action_name varchar2) is
  777.     begin icd_set_action(action_name); end;
  778.  
  779.   -- for some reason reading the module and the action in one ICD did not
  780.   -- work (I kept getting access violations).  Splitting them up into two
  781.   -- made it work.
  782.   procedure read_module(module_name out varchar2, action_name out varchar2) is
  783.   begin 
  784.     icd_read_module(module_name);
  785.     icd_read_action(action_name);
  786.   end;
  787.  
  788.   procedure set_client_info(client_info varchar2) is
  789.     begin icd_set_client_info(client_info); end;
  790.  
  791.   procedure read_client_info(client_info out varchar2) is
  792.     begin icd_read_info(client_info); end;
  793.  
  794. end;
  795. /
  796.  
  797. create or replace package body dbms_space is 
  798.  
  799.   procedure ktsbusp     (segment_owner IN varchar2, 
  800.                          segment_name IN varchar2,
  801.                          segment_type IN varchar2,
  802.                          total_blocks OUT number,
  803.                          total_bytes OUT number,
  804.                          unused_blocks OUT number,
  805.                          unused_bytes OUT number,
  806.                          last_used_extent_file_id OUT number,
  807.                          last_used_extent_block_id OUT number,
  808.                          last_used_block OUT number
  809.                          );
  810.   pragma interface(C, ktsbusp);                  -- 1 (see ktsb.c)
  811.  
  812.   procedure ktsbfbl     (segment_owner IN varchar2, 
  813.                          segment_name IN varchar2,
  814.                          segment_type IN varchar2,
  815.                          freelist_group_id IN number,
  816.                          free_blks OUT number,
  817.                          scan_limit IN number DEFAULT NULL
  818.                          );
  819.   pragma interface(C, ktsbfbl);                  -- 2 (see ktsb.c)
  820.  
  821.   procedure unused_space(segment_owner IN varchar2, 
  822.                          segment_name IN varchar2,
  823.                          segment_type IN varchar2,
  824.                          total_blocks OUT number,
  825.                          total_bytes OUT number,
  826.                          unused_blocks OUT number,
  827.                          unused_bytes OUT number,
  828.                          last_used_extent_file_id OUT number,
  829.                          last_used_extent_block_id OUT number,
  830.                          last_used_block OUT number
  831.                          ) IS
  832.   BEGIN
  833.     ktsbusp(segment_owner, segment_name, segment_type, total_blocks, 
  834.         total_bytes, unused_blocks, unused_bytes, last_used_extent_file_id, 
  835.         last_used_extent_block_id, last_used_block);
  836.   END unused_space;
  837.  
  838.   procedure free_blocks (segment_owner IN varchar2, 
  839.                          segment_name IN varchar2,
  840.                          segment_type IN varchar2,
  841.                          freelist_group_id IN number,
  842.                          free_blks OUT number,
  843.                          scan_limit IN number DEFAULT NULL
  844.                          ) IS
  845.   BEGIN
  846.     ktsbfbl(segment_owner, segment_name, segment_type, freelist_group_id, 
  847.         free_blks, scan_limit);
  848.   END free_blocks;
  849. end;
  850. /
  851.